package com.dp.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HttpTool
 * @Description:
 * @Auther: 田英杰
 * @Date: 2022/05/26 17:09
 * @Version: 1.0.0.0
 */
@Component
public class HttpTool {

    public static void http(@RequestParam String url, @RequestBody String object){
        try{
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            String data = AesGcm256Util.encrypt(object);
            JSONObject jsonObjecti = new JSONObject();
            jsonObjecti.put("data",data);
//            System.out.println("加密后的参数++++++++++" + data);
            String jsonString = JSON.toJSONString(jsonObjecti);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("Credit-Code","91140000717850635P");
            // 响应模型
            CloseableHttpResponse response = null;
            try {
                // 由客户端执行(发送)Post请求
                response = httpClient.execute(httpPost);
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                System.out.println("响应状态为:" + response.getStatusLine());
                if (responseEntity != null) {
                    System.out.println("响应内容长度为:" + responseEntity.getContentLength());
                    String s = EntityUtils.toString(responseEntity);
                    System.out.println("响应内容为:" +s);
                    //解析json
//                    System.out.println(s);
                    JSONObject jsonObject=JSONObject.parseObject(s);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 释放资源
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }
    }

    public static Map<String,Object> setData(String str){
        Map<String,Object> map = new HashMap<>();
        map.put("data",str);
        return map;
    }

    public static void http1(String url, String jsonstr,String jkname){
        try{
//            System.out.println("请求格式+++++++"+jsonstr);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(url);
            String data = jsonstr;
//            String data = AesGcm256Util.encrypt(jsonstr,AesGcm256Util.hexToByte("D5AA36E6795B488A992A9E67151BC90E"),AesGcm256Util.hexToByte("7845EA78F74C4D728996C6CF46C669B0"));//参数一：封装的数据，参数二：k，参数三：v

//            System.out.println(jkname+"===="+data);
            JSONObject jsonObjecti = new JSONObject();
            JSONObject jsonObject1=JSONObject.parseObject(data);
//            jsonObjecti.put("data-raw",jsonObject1);
            String jsonString = JSON.toJSONString(jsonObject1);
            StringEntity entity = new StringEntity(jsonString, "UTF-8");
//            System.err.println("------"+jsonString);
            // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/json;charset=utf8");
            httpPost.setHeader("Credit-Code","91140000717850635P");
            // 响应模型
            CloseableHttpResponse response = null;
            try {
                // 由客户端执行(发送)Post请求
                response = httpClient.execute(httpPost);
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                System.out.println(jkname+"===响应状态为:" + response.getStatusLine());
                if (responseEntity != null) {
                    System.out.println(jkname+"===响应内容长度为:" + responseEntity.getContentLength());
                    String s = EntityUtils.toString(responseEntity);
                    System.out.println(jkname+"===响应内容为:" +s);
                    //解析json
                    JSONObject jsonObject=JSONObject.parseObject(s);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 释放资源
                    if (httpClient != null) {
                        httpClient.close();
                    }
                    if (response != null) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e){
            e.printStackTrace();

        }
    }

     public static String doPost(String url,@RequestBody Map<String,Object> map){


        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        // 我这里利用阿里的fastjson，将Object转换为json字符串;
        // (需要导入com.alibaba.fastjson.JSON包)
        String jsonString = JSON.toJSONString(map);
        StringEntity entity = new StringEntity(jsonString, "UTF-8");
        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        // 响应模型
        CloseableHttpResponse response = null;
        HashMap<String, Object> map2 = new HashMap<>();
        String result="";
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity);
//                JSONObject jsonObject=JSONObject.parseObject(result);

//                JSONObject jsonObject1=JSONObject.parseObject(result);
//                result = jsonObject1.get("PHOTOPATH").toString();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;

    }

}

